from django import forms
import phonenumbers
from django.core.exceptions import ValidationError

class PhoneNumberForm(forms.Form):
    phone = forms.CharField(max_length=20)

    def clean_phone(self):
        phone = self.cleaned_data['phone']
        try:
            parsed = phonenumbers.parse(phone, None)
            if not phonenumbers.is_valid_number(parsed):
                raise ValidationError("请输入有效的手机号码")
            return phonenumbers.format_number(
                parsed, phonenumbers.PhoneNumberFormat.E164)
        except phonenumbers.NumberParseException:
            raise ValidationError("请输入有效的手机号码")

class VerificationCodeForm(forms.Form):
    code = forms.CharField(max_length=6)
